package edu.zut.cs.software.example.student.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.zut.cs.software.example.QueryInfo.QueryInfo;
import edu.zut.cs.software.example.service.impl.GenericManagerImpl;
import edu.zut.cs.software.example.student.dao.StudentDao;
import edu.zut.cs.software.example.student.domain.Student;
import edu.zut.cs.software.example.student.service.StudentManager;

/**
 * @author chen ming yang
 *
 */
@Service
public class StudenManagerImpl extends GenericManagerImpl<Student, Long> implements StudentManager {

	
	StudentDao studentDao;

	@Autowired
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
		this.dao = this.studentDao;
	}
	
	/**
	 * To use the loginService
	 * The student type parameter needs to be passed in front
	 * Returns student type data
	 */
	@Override
	public Student loginService(Student student) {
		return  studentDao.getByUserIdPassword(student.getUserId(), student.getPassword());
	}

	/**
	 * To use the getClassmatesService
	 * You need the front end to pass the QueryInfo type parameter
	 * Returns HashMap type data, including number of student, list of student information
	 */
	@Override
	public HashMap<String,Object> getClassmatesService(QueryInfo queryInfo) {
		int numbers = studentDao.getStudentCounts(queryInfo.getQuery());
		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
		List<Student> Students= studentDao.getClasStudent(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
		HashMap<String,Object> res=new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Students);
		return res;
	}

	
	/**
	 * To use the service
	 * You need to pass an int argument
	 * Returns student type data
	 */
	@Override
	public Student getUpdateStuService(int id) {
		return studentDao.getUpdateUser(id);
	}

	@Override
	public int editStuInfoService(Student student) {
		Long ID=student.getId();
		String major=student.getMajor();
		String grade=student.getGrade();
		
		return studentDao.editStudent(major, grade, ID);
	}

	@Override
	public HashMap<String,Object> getUserInfo(String userId) {
		Student student=studentDao.findByUserId(userId);
		HashMap<String, Object> map = new HashMap<>();
		map.put("class", student.getClas());
		map.put("grade", student.getGrade());
		map.put("id", student.getId());
		map.put("major",student.getMajor() );
		map.put("name", student.getName());
		map.put("state", student.getState());
		map.put("userId", student.getUserId());
		return map;
	}
	
	@Override
	public Student getStudent(String userId) {
		return studentDao.findByUserId(userId);
	}

	@Override
	public boolean findByuserId(String userId) {
		// TODO Auto-generated method stub
		return  studentDao.existsByUserId(userId);
	}

	@Override
	public Student  adduser(Student student) {
		// TODO Auto-generated method stub
		student.setState(true);
		studentDao.save(student);
		return null;
	}
	
	

}
